package servlet;

import bean.Result;
import biz.ProductTypeBiz;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveProductTypeServlet", value = "/SaveProductTypeServlet.s")
public class SaveProductTypeServlet extends HttpServlet {

    private ProductTypeBiz biz = new ProductTypeBiz();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String tname = request.getParameter("tname");

        final String tid = request.getParameter("tid");

        Result result;

        try {
            if (tid == null || "".equals(tid)){
                biz.saveProductType(tname);
            } else{
                biz.modifyProductType(tname, tid);
            }
            result = new Result(1, "保存成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(0, e.getMessage(),null);
        }

        final String json = new Gson().toJson(result);
        response.getWriter().append(json);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
